package com.qf.realm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class IniRealmTest {

    @Test
    public void ini(){
        //1.创建Realm
        IniRealm realm = new IniRealm("classpath:shiro.ini");

        //2.创建SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(realm);

        //        3. 创建Subject
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();


        //        4. 由subject发送认证请求.
        subject.login(new UsernamePasswordToken("admin","admin"));

        //5.角色的授权
        if (subject.hasRole("超级管理员")){
            System.out.println("给予超级管理员权限");
        }
        subject.checkRole("超级管理员");

        //6.权限的授权
        subject.checkPermissions("user:select","user:info");
        if(subject.isPermitted("user:info")){
            System.out.println("拥有user:info权限!!");
        }
    }

}
